﻿#pragma once
#include <list>
#include <mutex>
#include "IM_Socket_Server.h"

struct WriteBuffer 
{
	char *buffer;
	int	  size;
	WriteBuffer(char *buf, int size) { buffer = buf; size = size; }
	void release_buffer() { delete[] buffer; }
};

const int SocketProtocol_Tcp = 1;
const int SocketProtocol_Udp = 2;

enum SocketStateType
{
	//正在监听
	  SOCKET_TYPE_LISTEN	
	//已连接
	, SOCKET_TYPE_CONNECTED
	//关闭
	, SOCKET_TYPE_HALFCLOSE
};

class Pri_Socket
{
private:
	Pri_Socket(int socket_id,unsigned int context_handle,int protocol,int socket_fd);
	Pri_Socket(const Pri_Socket &) = delete;
	~Pri_Socket();

	//直接发送数据
	//不会释放发送数据的缓存
	//保证线程安全 
	bool socket_direct_send(char *buf,int size);
	//发送缓存 队列 中的数据
	//线程安全  
	int  socket_send_buffer();  
	//将数据 加入 发送缓存队列中  
	int  push_data_to_write_buffer(char * buffer,int size);

	bool nomore_send_data() { return m_write_buffer_list.size() == 0; }

	void set_socket_noblocking();
	void set_secket_keepalive();

	/*****************调试使用 *******************/

	//获取当前socket 的状态  返回文本说明   
	//用于调试 
	const char * get_socket_state_text();
	//打印当前socekt 的信息 
	void print_socket_infor();
	
/**********************************************/
private:
	int m_socket_id;   //socket 的应用层id
	int m_socket_fd;
	unsigned int m_context_handle;
	std::list<WriteBuffer>  m_write_buffer_list;
	int   m_protocol;     //socket 的协议  
	SocketStateType  m_socket_sate_type;   //socket 目前的状态  
	std::mutex   m_write_buffer_mutex;		//发送缓存区的互斥锁 
	int m_read_size; 
	friend class IM_Socket_Server;
};

